import requests
import os
import time


def get_video(url):
    """ spider video
    :param url: video url
    :return:
    """
    root = "D://video//"
    path = root + str(int(time.time())) + ".mp4"  # 抓取文件起的名字
    try:
        if not os.path.exists(root):
            os.mkdir(root)  # 如果该目录不存在就创建它
        if not os.path.exists(path):
            r = requests.get(url)  # 获取到目标视频的所有信息
            print(r.status_code)  # 打印访问的状态码是否为200
            with open(path, 'wb') as f:  # 以二进制写的方式将r的二进制内容写入path
                f.write(r.content)
                f.close()
                print("文件保存成功")
        else:
            print("文件已存在")
    except:
        print("爬取失败")

